﻿using Frameworks;
using UnityEngine;
using UnityEngine.SceneManagement;
//using Debugger = Frameworks.Debugger;

public class PreloadScene : MonoBehaviour
{
    public string FirstLoadSceneName = "FirstScene";    // 方便进入游戏时跳转到指定的游戏场景
    public string LoadingSceneName = "Loading";         // 加载场景的进度显示的场景名称
    public GUI_ID FirstModule;

    private void Awake()
    {
        DontDestroyOnLoad(this);
        Initialize();
    }

    private void Initialize()
    {
        //Screen.fullScreen = true;
        //Screen.SetResolution(1920, 1080, true);
        //QualitySettings.SetQualityLevel(4);
        //QualitySettings.antiAliasing = 4;
        //Application.targetFrameRate = 60;
        //RenderSettings.ambientLight = Color.white;
        //Input.multiTouchEnabled = false;
        //GameGlobal.TIME_START_GAME = System.DateTime.Now.Ticks;
        //Application.runInBackground = true;
        //Screen.sleepTimeout = SleepTimeout.NeverSleep;

        //Debugger.EnableDebugLogs(true);
        Frameworks.Debugger.EnableOnScreen(true);
        //Debugger.EnableOnText(true);

        GameManager.Instance.Initialize(this, FirstLoadSceneName);
        GameManager.SceneMgr.SetupLoadingScene(LoadingSceneName);
    }

    private void Start()
    {
        UnityEngine.SceneManagement.SceneManager.sceneLoaded += OnFirstSceneLoaded;

        GameManager.Instance.Start();
        GameManager.SceneMgr.LoadSceneDirect(FirstLoadSceneName);
    }

    private void OnFirstSceneLoaded(Scene sceneName, LoadSceneMode mode)
    {
        if (sceneName.name != FirstLoadSceneName) return;

        UnityEngine.SceneManagement.SceneManager.sceneLoaded -= OnFirstSceneLoaded;
        if (FirstModule != GUI_ID.None) GameManager.GUIMgr.Display(FirstModule);
    }

    private void Update()
    {
#if UNITY_EDITOR
        if (Input.GetKeyDown(KeyCode.P))
        {
            Debug.Log("Pause program!");
            UnityEditor.EditorApplication.isPaused = true;
        }
        if (Input.GetKeyDown(KeyCode.Escape))
        {
            //Debug.Log("Stop program!");
            UnityEditor.EditorApplication.isPlaying = false;
        }
#else
        if (Input.GetKeyDown(KeyCode.Escape))
        {
            Debug.Log("Exit program!");
            System.Diagnostics.Process.GetCurrentProcess().Kill();
            System.Environment.Exit(0);
            Application.Quit();
        }
#endif

        GameManager.Instance.Update();
    }

    private void FixedUpdate()
    {
        GameManager.Instance.FixedUpdate();
    }

    private void OnApplicationPause(bool isPause)
    {
        GameManager.Instance.OnApplicationPause(isPause);
    }

    private void OnApplicationQuit()
    {
        GameManager.Instance.OnApplicationQuit();
    }

    private void OnDestroy()
    {
        Debug.Log("Close program!");
        GameManager.Instance.Destroy();
    }

    [RuntimeInitializeOnLoadMethod]
    private static void StartupScene()
    {
        if (UnityEngine.SceneManagement.SceneManager.GetActiveScene().name != "Preload")
        {
            UnityEngine.SceneManagement.SceneManager.LoadScene("Preload");
        }
    }
}